package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentUserSexBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserUpdateInfoAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/6/22.
 */
public class UserSexActivity extends AbsTitleBarActivity {

    public static void start(Context context, String original) {
        Intent intent = new Intent(context, UserSexActivity.class);
        intent.putExtra("sex", original);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return "性别";
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new UserSexFragment();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putString("sex", getIntent().getStringExtra("sex"));
        return args;
    }
    @Override
    protected void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(IndividualEvent event) {
        finish();
    }

    public static class UserSexFragment extends BaseFragment {
        private FragmentUserSexBinding mDataBinding;
        private String mOriginalSex;
        private RadioGroup.OnCheckedChangeListener mOnCheckChangerListener = new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rbfemale= (RadioButton) group.findViewById(R.id.rb_female);
                rbfemale.setTextColor(getResources().getColor(R.color.gray));
                RadioButton rbmale= (RadioButton) group.findViewById(R.id.rb_male);
                rbmale.setTextColor(getResources().getColor(R.color.gray));
                if (R.id.radio_group_sex == group.getId()) {
                    RadioButton rbSex = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
                    rbSex.setTextColor(getResources().getColor(R.color.theme_color));
                    String checkedItem = rbSex.getText().toString();
                    PbBean.PbSignUserSex sex = getString(R.string.usr_sex_male_me).equals(checkedItem) ? PbBean.PbSignUserSex.USER_SEX_MAN : PbBean.PbSignUserSex.USER_SEX_WOMAN;
                    if(TextUtils.equals(checkedItem,mOriginalSex)){
                        getActivity().finish();
                    }else {
                        updateUserInfo(sex);
                    }
                }
//                else if (R.id.radio_group_dress_style == group.getId()) {
//                    RadioButton rbStyle = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
//                    String checkedItem = rbStyle.getText().toString();
//                    if (getString(R.string.usr_style_male).equals(checkedItem)) {
//                        mClothingStyle = PbBean.PbSignUserClothingStyle.USER_CLOTHING_STYLE_MAN;
//                    } else if (getString(R.string.usr_style_female).equals(checkedItem)) {
//                        mClothingStyle = PbBean.PbSignUserClothingStyle.USER_CLOTHING_STYLE_WOMAN;
//                    } else if (getString(R.string.usr_style_normal).equals(checkedItem)) {
//                        mClothingStyle = PbBean.PbSignUserClothingStyle.USER_CLOTHING_STYLE_MIDDLE;
//                    }
//                }
            }
        };
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mDataBinding = FragmentUserSexBinding.inflate(inflater, container, false);
            initViews();
            return mDataBinding.getRoot();
        }
        private void initViews() {
            mOriginalSex = getArguments().getString("sex", "");
            mDataBinding.radioGroupSex.setOnCheckedChangeListener(mOnCheckChangerListener);
        }
        private void updateUserInfo(PbBean.PbSignUserSex sex) {
            showProgressDialog();
            onNewRequestCall(UserUpdateInfoAction.newInstance(getContext(), sex).request(new AHttpService.IResCallback<PbBean.PbResUserUpdateInfo>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResUserUpdateInfo> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if(getView() != null) {
                        closeProgressDialog();
                        if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                            return;
                        }
                        PbBean.PbResUserUpdateInfo info = response.body();
                        IndividualEvent event = new IndividualEvent(IndividualEvent.USER_SEX_EVENT);
                        event.userInfo = info.getUserInfo();
                        EventBusUtil.post(event);
                    }
                }
            }));
        }
    }
}
